package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminSubTypeDao;
import ex.com.jm.food.entity.SubType;
import ex.com.jm.food.entity.SupType;

/**
 * 
 * @author zhaojw
 *
 */
public class AdminSubTypeDaoImpl extends HibernateDaoSupport implements
		AdminSubTypeDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<SubType> getSubTypes(final int initPage,final int pageRow,final String name,final Integer supId) {
		// TODO Auto-generated method stub
		Object subType=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.SubTypeDTO(sub.subId,sub.subName,sub.created) from SubType sub where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql+=" and sub.subName like '%"+name+"%'";
				}if(supId!=null&&!supId.equals("")){
					hql+=" and sub.supId="+supId+" ";
				}
				hql+=" order by sub.created desc ";
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<SubType>) subType;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int subTypeCount(final String name,final Integer supId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from SubType sub where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql+=" and sub.subName like '%"+name+"%'";
				}if(supId!=null&&!supId.equals("")){
					hql+=" and sub.supId="+supId+" ";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public Object saveSubType(SubType subType) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(subType);
	}

	@Override
	public void updateSubType(SubType subType) {
		// TODO Auto-generated method stub
           this.getHibernateTemplate().update(subType);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public SubType checkName(final String name) {
		// TODO Auto-generated method stub
		SubType subType=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stubb
				String hql="from SubType sub where sub.subName='"+name+"'";
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return subType;
	}

	@Override
	public SubType editSubType(Integer id) {
		// TODO Auto-generated method stub
		
		return this.getHibernateTemplate().get(SubType.class, id);
	}

	@Override
	public void delSubType(Integer id) {
		// TODO Auto-generated method stub
    this.getHibernateTemplate().delete(this.getHibernateTemplate().get(SubType.class, id));
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<SupType> getSupTypes() {
		// TODO Auto-generated method stub
		Object supTypes=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from SupType");
				return query.list();
			}
		});
		return (List<SupType>) supTypes;
	}

	@Override
	public SupType getSupType(Integer supId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(SupType.class, supId);
	}

}
